﻿


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using XT.Common.Extensions;

namespace IotContract.Extensions;
public static class SafeExtension
{
    public static string ToSafe(this string data)
    {
        return data ?? "0";
    }
    public static async Task<T> ToResult<T>(this HttpResponseMessage response)
    {
        var responseAsString = await response.Content.ReadAsStringAsync();
        var responseObject =responseAsString.ToObject<T>();
        return responseObject;
    }
    /// <summary>
    /// 通道变为数字
    /// </summary>
    /// <param name="channels"></param>
    /// <returns></returns>
    public static (int,string) ChannelToNumber(this IEnumerable<int> channels)
    {
        StringBuilder str = new StringBuilder();
        foreach(var channel in channels)
        {
            if (channel == 2)
            {
                str.Append("0");
            }
            else
            {
                str.Append("1");
            }
            
        }
        UInt16 result = 0;
        UInt16.TryParse(str.ToString(), System.Globalization.NumberStyles.BinaryNumber, null, out result);

        var bigEndianValue = IPAddress.HostToNetworkOrder((UInt16)result);
        // UInt16.TryParse()
        return (bigEndianValue,str.ToString());
    }
}
